package com.itestor.iptv.modules.common.actions;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.itestor.iptv.global.GlobalConstant;
import com.itestor.iptv.modules.common.utils.CommonConstant;
import com.itestor.iptv.modules.common.vo.User;
import com.itestor.iptv.modules.ne.service.RobotManageService;
import com.itestor.iptv.modules.ne.vo.RobotNodeStatusBean;

/**
 * 
 * @author Mr.li
 * 
 */
public class HomeMainAction extends Action {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger.getLogger(HomeMainAction.class);

    /**
     * 
     */
    private RobotManageService robotManageService;

    /**
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     * 
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String timeParam = request.getParameter("time");

        User user = (User) request.getSession().getAttribute(
                CommonConstant.SESSION_ATTR_USER);
        
        Calendar curCalendar = Calendar.getInstance();
        Date currentTime = curCalendar.getTime();

        // 10分钟
        Date lastTime = new Date(currentTime.getTime() - 600000);

        if (!StringUtils.isEmpty(timeParam)) {
            if ("5m".equals(timeParam)) {
                lastTime = new Date(currentTime.getTime() - 300000);
            } else if ("10m".equals(timeParam)) {
                lastTime = new Date(currentTime.getTime() - 600000);
            } else if ("15m".equals(timeParam)) {
                lastTime = new Date(currentTime.getTime() - 900000);
            } else if ("30m".equals(timeParam)) {
                lastTime = new Date(currentTime.getTime() - 1800000);
            }
        }

        System.out.println(": " + user.getId());

        // 获取节点状态列表
        Map<Integer, Map<Integer, RobotNodeStatusBean>> nodeStatusMap = robotManageService
                .getNodeStatusListByUser(user.getId(), lastTime);

        // 获取告警节点列表
        Map<Integer, RobotNodeStatusBean> nodeAlertMap = robotManageService
                .getNodeAlertListByUser(user.getId(), lastTime, currentTime);

        request.setAttribute("ONLINE_NUM", nodeStatusMap.get(
                RobotNodeStatusBean.NODE_STATUS_ONLINE).size());
        request.setAttribute("OFFLINE_NUM", nodeStatusMap.get(
                RobotNodeStatusBean.NODE_STATUS_OFFLINE).size());
        request.setAttribute("ONLINE_ALERT_NUM", getNodeStatusAlertsNum(
                nodeAlertMap, nodeStatusMap
                        .get(RobotNodeStatusBean.NODE_STATUS_ONLINE)));
        request.setAttribute("OFFLINE_ALERT_NUM", getNodeStatusAlertsNum(
                nodeAlertMap, nodeStatusMap
                        .get(RobotNodeStatusBean.NODE_STATUS_OFFLINE)));

        // 获取TOPN告警列表
        List<RobotNodeStatusBean> nodeAlertList = robotManageService
                .getNodeStatusAlertsByUser(user.getId(), lastTime, currentTime,
                        20);
        request.setAttribute("RobotNodeAlerts", nodeAlertList);

        // 获取节点信息列表
        List<RobotNodeStatusBean> nodeDataList = robotManageService
                .getLastNodeDataListByUser(user.getId());
        request.setAttribute("RobotNodeDataList", nodeDataList);

        return mapping.findForward(GlobalConstant.FORWARD_SUCCESS);
    }

    /**
     * 获取节点告警数量
     * @param nodeAlertMap
     * @param nodeStatusMap
     * @return
     */
    private int getNodeStatusAlertsNum(
            Map<Integer, RobotNodeStatusBean> nodeAlertMap,
            Map<Integer, RobotNodeStatusBean> nodeStatusMap) {
        int count = 0;
        for (Entry<Integer, RobotNodeStatusBean> entry : nodeStatusMap
                .entrySet()) {
            if (nodeAlertMap.containsKey(entry.getKey())) {
                count = count + 1;
            }
        }
        return count;
    }

    /**
     * @param pRobotManageService
     */
    public void setRobotManageService(RobotManageService pRobotManageService) {
        robotManageService = pRobotManageService;
    }

}